package com.audi.adapter;

import java.util.ArrayList;

import com.audi.R;
import com.audi.adapter.ListPlaceCardAdapter.ViewHolder;
import com.audi.dto.HomeControlPlaceCard;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class HomeControlPlaceAdapter extends BaseAdapter implements OnClickListener {
    private HomeControlPlaceCard mHomeControlPlaceCard;
    private ArrayList<HomeControlPlaceCard> listHomeControlPlaceCard;
    private Activity mActivity;

    public HomeControlPlaceAdapter(Activity activity,
	    ArrayList<HomeControlPlaceCard> listHomeControlPlaceCard) {
	this.mActivity = activity;
	this.listHomeControlPlaceCard = listHomeControlPlaceCard;
    }

    @Override
    public int getCount() {
	return listHomeControlPlaceCard.size();
    }

    @Override
    public Object getItem(int position) {
	return listHomeControlPlaceCard.get(position);
    }

    @Override
    public long getItemId(int position) {
	return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewgroup) {
	HomeControlPlaceCard item = listHomeControlPlaceCard.get(position);
	ViewHolder viewholder = null;
	if (convertView == null) {
	    viewholder = new ViewHolder();
	    convertView = LayoutInflater.from(mActivity).inflate(
		    R.layout.row_home_control, null);
	    viewholder.iconHomeControl = (ImageView) convertView
		    .findViewById(R.id.icon_home_control);
	    viewholder.namePlaceHomeControl = (TextView) convertView
		    .findViewById(R.id.tv_name_place);
	    viewholder.menuPlaceHomControl = (ImageView) convertView
		    .findViewById(R.id.btn_menu_place);
	    viewholder.placeCapbability = (TextView) convertView
		    .findViewById(R.id.tv_place_capbability);
	    convertView.setTag(viewholder);
	} else {
	    viewholder = (ViewHolder) convertView.getTag();
	}
	/*
	 * set Data for View
	 */
	viewholder.iconHomeControl
		.setImageResource(R.drawable.icon_plug_on_off);
	viewholder.namePlaceHomeControl.setText(item.getNamePlaceHomeControl());
	viewholder.menuPlaceHomControl
		.setImageResource(R.drawable.ic_drawer_menu);
	viewholder.placeCapbability.setText(item.getPlaceCapability());
	
	/*
	 *  set Onclick menu place
	 */
	viewholder.menuPlaceHomControl.setOnClickListener(this);

	return convertView;
    }

    static class ViewHolder {
	private ImageView iconHomeControl;
	private TextView namePlaceHomeControl;
	private TextView placeCapbability;
	private ImageView menuPlaceHomControl;
    }

    @Override
    public void onClick(View view) {
	if(view.getId()==R.id.btn_menu_place)
	{
	   pMenuPlace(view);
	}
	
    }
    public void pMenuPlace(View view)
    {
	PopupMenu pMenuPlace = new PopupMenu(mActivity,view );
	pMenuPlace.getMenuInflater().inflate(R.menu.popup_menu_place_card,pMenuPlace.getMenu());
	pMenuPlace.show();
    }
}
